﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Warehouse.Web.AppCode;
using System.Web.Script.Serialization;
using System.Data.SqlClient;

namespace Warehouse.Web.Management
{
    public partial class Warehouses : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected override void ProcessAjaxRequest(HttpContext context, string action)
        {
            switch (action)
            {
                case "GetHouse":
                    {
                        int id = int.Parse(context.Request.QueryString["HouseID"]);
                        DataAccess.Warehouse house = DataAccess.Warehouse.GetWarehouse(id);
                        if (house == null)
                        {
                            WebUtil.WriteJsonError(context, "仓库已经被删除");
                        }
                        else
                        {
                            WebUtil.WriteJsonResult(context, house);
                        }
                    }
                    break;
                case "AddHouse":
                    {
                        string name = context.Request.Form["Name"].Trim();
                        string description = context.Request.Form["Description"];
                        DataAccess.WarehouseType houseType = (DataAccess.WarehouseType)int.Parse(context.Request.Form["HouseType"]);
                        if (string.IsNullOrEmpty(name))
                        {
                            WebUtil.WriteJsonError(context, "仓库名不能为空");
                            return;
                        }
                        try
                        {
                            DataAccess.Warehouse house = new Warehouse.DataAccess.Warehouse()
                            {
                                Name = name,
                                HouseType = houseType,
                                Description = description
                            };
                            house.Save();
                            WebUtil.WriteJsonResult(context, null);
                        }
                        catch (SqlException ex)
                        {
                            if (ex.Number == 2601)
                            {
                                WebUtil.WriteJsonError(context, "该仓库名已存在，请更改名称后再保存");
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                    break;
                case "SaveHouse":
                    {
                        int id = int.Parse(context.Request.Form["ID"]);
                        string name = context.Request.Form["Name"].Trim();
                        string description = context.Request.Form["Description"];
                        DataAccess.WarehouseType houseType = (DataAccess.WarehouseType)int.Parse(context.Request.Form["HouseType"]);
                        if (string.IsNullOrEmpty(name))
                        {
                            WebUtil.WriteJsonError(context, "仓库名不能为空");
                            return;
                        }
                        DataAccess.Warehouse house = DataAccess.Warehouse.GetWarehouse(id);
                        if (house == null)
                        {
                            WebUtil.WriteJsonError(context, "仓库已经被删除");
                            return;
                        }
                        try
                        {
                            house.Name = name;
                            house.HouseType = houseType;
                            house.Description = description;

                            house.Save();
                            WebUtil.WriteJsonResult(context, null);
                        }
                        catch (SqlException ex)
                        {
                            if (ex.Number == 2601)
                            {
                                WebUtil.WriteJsonError(context, "该仓库名已存在，请更改名称后再保存");
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                    break;
                case "DeleteHouse":
                    {
                        int id = int.Parse(context.Request.Form["HouseID"]);
                        DataAccess.Warehouse house = DataAccess.Warehouse.GetWarehouse(id);
                        if (house != null)
                        {
                            try
                            {
                                house.Delete();
                                WebUtil.WriteJsonResult(context, null);
                            }
                            catch (SqlException ex)
                            {
                                if (ex.Number == 547)
                                {
                                    WebUtil.WriteJsonError(context, "该仓库已存在入（出）库记录，不能删除");
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }
                        else
                        {
                            WebUtil.WriteJsonResult(context, null);
                        }
                    }
                    break;
                default:
                    base.ProcessAjaxRequest(context, action);
                    return;
            }
        }

        public string GetWarehouseType(DataAccess.WarehouseType type)
        {
            switch (type)
            {
                case Warehouse.DataAccess.WarehouseType.Virtual:
                    return "虚拟仓库";
                case Warehouse.DataAccess.WarehouseType.Entity:
                    return "实体仓库";
                case Warehouse.DataAccess.WarehouseType.Unkown:
                default:
                    return "未知类型";
            }
        }

        void LoadData()
        {
            this.rptHouses.DataSource = DataAccess.Warehouse.GetWarehouses();
            this.rptHouses.DataBind();
        }
    }
}
